#include <Arduino.h>
#include <SPI.h>
#include <FS.h>
#include <SPIFFS.h>
#include <TFT_eSPI.h>
#include "images.h"
#include "TFTDriver.h"

TFTHandle tft = TFTHandle();

TFTHandle::TFTHandle()
{
}
void TFTHandle::init()
{
    // 初始化背光控制引脚
    pinMode(TFT_BL, OUTPUT);
    ESP_LOGI("TFTDriver", "TFT driver creating");
    setLight(64); // 设置屏幕初始亮度为一半
    ESP_LOGI("TFTDriver", "TFT driver creating");
    // 初始化屏幕显示
    TFT.init();
    ESP_LOGI("TFTDriver", "TFT driver creating");
    TFT.setRotation(1); // 顺时针旋转90度
    ESP_LOGI("TFTDriver", "TFT driver creating");
    TFT.initDMA();              // 初始化 DMA 驱动
    TFT.setSwapBytes(true);     // 图像像素点字节交换，否则会色彩错误（若要使用eSprite，则必须用eSprite对象调用此方法）
    eSprite.setSwapBytes(true); // 图像像素点字节交换，否则会色彩错误（若要使用eSprite，则必须用eSprite对象调用此方法）
    // 初始化触摸
    TFT.setTouch(calibrationData); // 设置触摸校准
}
/*
绘制背景
*/
void TFTHandle::drawImg(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t *img)
{
    // // 保留背景图操作
    // eSprite.createSprite(w, h);
    // eSprite.pushImage(-x, -y, BKImage_w, BKImage_h, BKImage);
    // eSprite.pushSprite(x, y);
    // eSprite.deleteSprite();

    eSprite.createSprite(w, h);
    eSprite.fillScreen(TFT_WHITE);
    eSprite.pushImage(0, 0, w, h, img);
    // eSprite.pushSprite(x, y, TFT_WHITE); // 对白色透明
    eSprite.pushSprite(x, y);
    eSprite.deleteSprite();
}
/*
PWM 设置屏幕亮度
pwmVal: 亮度值，范围0-255
*/
void TFTHandle::setLight(uint8_t pwmVal)
{
    analogWrite(TFT_BL, pwmVal);
}
/*
设置背景图数组
*/
void TFTHandle::setBKI(uint16_t w, uint16_t h, uint16_t *img)
{
    BKImage = img;
    BKImage_w = w;
    BKImage_h = h;
}
void TFTHandle::drawBKI()
{
    // TFT.pushImage(0, 0, BKImage_w, BKImage_h, BKImage); // 显示背景图片
    TFT.fillScreen(TFT_WHITE);
    drawImg(26, 65, 117, 23, (uint16_t *)img_XXCKT);
    drawImg(20, 10, 130, 50, (uint16_t *)img_aircondition);
    drawImg(mode_x + 10, mode_y - 40, 60, 27, (uint16_t *)img_moshi);
    drawImg(mode_x, mode_y, icon_w, icon_h, (uint16_t *)img_wram);
    drawImg(wind_x + 10, wind_y - 40, 60, 27, (uint16_t *)img_fengsu);
    drawImg(wind_x, wind_y, icon_w, icon_h, (uint16_t *)img_wind1);
    drawImg(290, 20, 142, 27, (uint16_t *)img_CNWSD);
    // 画一条线分隔模式和加减档
    TFT.drawLine(0, tempp_y - 9, TFT_HEIGHT, tempp_y - 9, TFT_BLACK);
    TFT.drawLine(0, tempp_y - 10, TFT_HEIGHT, tempp_y - 10, TFT_BLACK);
    drawImg(tempp_x, tempp_y, 50, 50, (uint16_t *)img_add);
    drawImg(temps_x, temps_y, 50, 50, (uint16_t *)img_sub);
    changeTemp(temp_value);
    changeCNWSD(26.3, 92);
}
/*
获取温度值
*/
uint8_t TFTHandle::getTemp(void)
{
    return temp_value;
}
/*
获取风速
*/
uint8_t TFTHandle::getWind(void)
{
    return wind_level;
}
/*
获取空调工作状态
*/
uint8_t TFTHandle::getStatus(void)
{
    return work_state;
}
/*
获取工作模式
*/
uint8_t TFTHandle::getMode(void)
{
    return mode_flag;
}
/*
获取触摸位置信息
*/
bool TFTHandle::getTouch(uint16_t *x, uint16_t *y)
{
    if (TFT.getTouch(x, y))
    {
        if (mode_x < *x && *x < mode_x + icon_w && mode_y < *y && *y < mode_y + icon_h)
        {
            changeMode((mode_flag + 1) % 3);
            vTaskDelay(300);
        }
        else if (wind_x < *x && *x < wind_x + icon_w && wind_y < *y && *y < wind_y + icon_h)
        {
            changeWind((wind_level + 1) % 4);
            vTaskDelay(300);
        }
        else if (tempp_x < *x && *x < tempp_x + 50 && tempp_y < *y && *y < tempp_y + 50)
        {
            if (temp_value < 30)
            {
                temp_value += 1;
                changeTemp(temp_value);
                vTaskDelay(300);
            }
        }
        else if (temps_x < *x && *x < temps_x + 50 && temps_y < *y && *y < temps_y + 50)
        {
            if (temp_value > 16)
            {
                temp_value -= 1;
                changeTemp(temp_value);
                vTaskDelay(300);
            }
        }
        return true;
    }
    return false;
}
/*
更换模式
mode 0-2间切换
*/
void TFTHandle::changeMode(uint8_t flag)
{
    mode_flag = flag;
    switch (mode_flag)
    {
    case 0:
        drawImg(mode_x, mode_y, icon_w, icon_h, (uint16_t *)img_wram);
        break;
    case 1:
        drawImg(mode_x, mode_y, icon_w, icon_h, (uint16_t *)img_cold);
        break;
    case 2:
        drawImg(mode_x, mode_y, icon_w, icon_h, (uint16_t *)img_dehumidification);
        break;
    }
}
/*
更换风速
mode 0-3间切换
*/
void TFTHandle::changeWind(uint8_t level)
{
    wind_level = (wind_level + 1) % 4;
    switch (wind_level)
    {
    case 0:
        drawImg(wind_x, wind_y, icon_w, icon_h, (uint16_t *)img_wind1);
        break;
    case 1:
        drawImg(wind_x, wind_y, icon_w, icon_h, (uint16_t *)img_wind1);
        break;
    case 2:
        drawImg(wind_x, wind_y, icon_w, icon_h, (uint16_t *)img_wind2);
        break;
    case 3:
        drawImg(wind_x, wind_y, icon_w, icon_h, (uint16_t *)img_wind3);
        break;
    }
}
/*
更新空调温度
*/
void TFTHandle::changeTemp(uint8_t temp_value)
{
    this->temp_value = temp_value;
    // // 保留原背景
    // eSprite.createSprite(120, 120);
    // eSprite.pushImage(-temp_x, -temp_y, BKImage_w, BKImage_h, BKImage);
    // eSprite.pushSprite(temp_x, temp_y);
    // eSprite.deleteSprite();
    eSprite.createSprite(220, TFT_HEIGHT - temp_y);
    eSprite.fillScreen(TFT_BLACK);
    eSprite.pushImage(5, 20, 105, 24, (uint16_t *)img_KTWD);
    eSprite.pushImage(170, 70, 32, 31, (uint16_t *)img_SSD);
    char str[3];
    sprintf(str, "%d", this->temp_value);
    eSprite.setTextColor(TFT_PINK, TFT_BLACK);
    eSprite.setTextSize(7);
    eSprite.drawString(str, 40, 60, 2);

    // eSprite.pushSprite(temp_x, temp_y, TFT_WHITE);
    // 挂起调度器
    eSprite.pushSprite(temp_x, temp_y);
    eSprite.deleteSprite();
}
/*
更新舱内温湿度数值
*/
void TFTHandle::changeCNWSD(float temp, uint8_t humi)
{
    char str[16];
    sprintf(str, "%.1fC/%.2f", temp, humi * 0.01);
    eSprite.createSprite(200, 30);
    eSprite.fillScreen(TFT_WHITE);
    eSprite.setTextSize(3);
    eSprite.setTextColor(TFT_BLACK, TFT_WHITE);
    eSprite.drawString(str, 0, 0, 1);
    eSprite.pushSprite(280, 60, TFT_WHITE);
}
/*
关机
*/
void TFTHandle::close(void)
{
    work_state = 0;
    setLight(10);
}
/*
开机
*/
void TFTHandle::open(void)
{
    work_state = 1;
    setLight(255);
}
